package com.service.business.member.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.service.business.member.controller.rep.MemberRWRep;
import com.service.business.member.controller.req.RWRecordPageReq;
import com.service.business.support.SessionUtil;
import com.service.business.utils.DateUtil;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.RwRecordEnum;
import com.service.dao.mapper.RwRecordMapper;
import com.service.business.member.service.MemberRWService;
import com.service.dao.model.RwRecord;
import com.service.utility.modelmapper.ModelMapperUtil;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年09月24日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class MemberRWServiceImpl extends ServiceImpl<RwRecordMapper, RwRecord> implements MemberRWService {

    @Autowired
    private SessionUtil sessionUtil;

    @Autowired
    private RwRecordMapper rwRecordMapper;

    @Override
    public CommonPageRep<MemberRWRep> pageRWRecord(RWRecordPageReq pageReq, Integer optTpe) {
        Page<RwRecord> page = PageHelper.startPage(pageReq.getPageNo(), pageReq.getPageSize());
        rwRecordMapper.pageRecord4App(sessionUtil.getUser().getUserId(), pageReq.getCoins().getCode(), optTpe);
        List<MemberRWRep> resultList = page.getResult().stream().map(x -> ModelMapperUtil.getStrictModelMapper().map(x, MemberRWRep.class))
                .collect(Collectors.toList());
        return CommonPageRep.buildResultRep(page, resultList);
    }

    @Override
    public BigDecimal getSomedayWithdrawNum(Integer memberId, CoinsEnum coins, Date someday) {
        Date startTime = DateUtil.getDayOfStart(someday);
        Date endTime = DateUtil.getDayOfEnd(someday);
        return baseMapper.getMemberTotalRwNumByTimeAndCoins(memberId,
                RwRecordEnum.OptType.withdraw.getCode(), coins.getCode(), startTime, endTime);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doAddRWRecord() {

    }


}
